/*
 * Participance.java
 *
 * Created on 4. September 2007, 17:02
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package timeitj.persistence;

import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Version;
import org.apache.log4j.Logger;

/**
 *
 * @author kunee
 */
@Entity
@NamedQueries({
    @NamedQuery(
    name="ParticipancesByPerson",
        query="select p from Participance p WHERE p.person= :person"),
        @NamedQuery(
    name="ParticipancesByProject",
        query="select p from Participance p WHERE p.project= :project")
})
@Table(name="PARTICIPANCE")
public class Participance implements java.io.Serializable {
    
    private static Logger logger = Logger.getLogger(Participance.class.getName());
    
    @Id @GeneratedValue
    @Column(name="ID")
    private long id;
    
    @Version
    @Column(name="OPT_LOCK")
    private Long version;
    
    @ManyToOne
    private Person person;
    
    @ManyToOne
    private Project project;
    
    @OneToMany(mappedBy = "participance")
    private List<TimeUnit> timeUnits;
    
    /** Creates a new instance of Participance */
    public Participance() {
    }
    
    public Participance(Person person, Project project) {
        this.person = person;
        this.project = project;
        logger.info("Person '" + person.getLogin() + "' joined Project '" + project.getName() +"'");
    }
    
    public long getId() {
        return id;
    }
        
    public Person getPerson() {
        return person;
    }
    
    public void setPerson(Person person) {
        this.person = person;
    }
    
    public Project getProject() {
        return project;
    }
    
    public void setProject(Project project) {
        this.project = project;
    }
    
    public List<TimeUnit> getTimeUnits() {
        return timeUnits;
    }
    
    public void setTimeUnits(List<TimeUnit> timeUnits) {
        this.timeUnits = timeUnits;
    }
    
    public void addTimeUnit(TimeUnit timeUnit) {
        timeUnits.add(timeUnit);
        logger.info("Person '" + person.getLogin() + "' booked new time on Project '" + project.getName() +"', starting at " + timeUnit.getStartDate());
    }
    
}
